﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RobotGameData;

namespace RobotGame.ScreenManagers
{
    public class GameplayScreen : GameScreen
    {
        private MapEngine mapEngine;
        private Camera camera;

       // private InputState input;
        private SpriteFont font;

        private PathFinder pathFinder;

        private MenuPanel menuPanel;

        private MiniMap miniMap;
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

          //  input = new InputState();
            mapEngine = new MapEngine();

            font = content.Load<SpriteFont>("Fonts\\menufont");

            String strPath = "Content\\Maps\\Map1.xml";
            mapEngine.Map = mapEngine.LoadMap(content, strPath);


            camera = new Camera(new Vector2(mapEngine.Map.MapDimensions.X, mapEngine.Map.MapDimensions.X),
                new Vector2(mapEngine.Map.TileSize.X, mapEngine.Map.TileSize.Y), 
                new Vector2(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height));

            World.Player = new FightingRobot(new Vector2(600, 600), new Vector2(138f, 192f), 6);
            World.Player.LoadContent(content, "Textures\\Players\\Warrior1WalkLeft");

            camera.FocusOn(World.Player);

            LoadClipMap();

            positionMouse = new Vector2(600, 600);

            mapEngine.Map.StartTile = mapEngine.Map.PointToTile(World.Player.Position);
            mapEngine.Map.EndTile = mapEngine.Map.PointToTile(World.Player.Position);
            pathFinder = new PathFinder();
            pathFinder.Initialize(mapEngine.Map);
            pathFinder.Reset();
            pathFinder.LoadContent(content);

            World.Player.Position = mapEngine.Map.MapToWorld(mapEngine.Map.StartTile, true);

            menuPanel = new MenuPanel(ScreenManager);
            menuPanel.LoadContent();


            miniMap = new MiniMap(this.ScreenManager,World.Player,new Point(750,2));
            miniMap.LoadContent();
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override void HandleInput(InputState input)
        {
            World.Player.HandleInput(input);
            camera.Update(input);

            if (input.IsRightButtonPress())
            {
                LoadingScreen.Load(ScreenManager, true, new GameTwoScreen());
                return;
            }

            if (pathFinder.SearchStatus == SearchStatus.PathFound && !World.Player.Moving)
            {
                foreach (Point point in pathFinder.FinalPath())
                {
                    World.Player.Waypoints.Enqueue(mapEngine.Map.MapToWorld(point, true));
                }
                World.Player.Moving = true;
            }

            if (input.IsLeftButtonPress()
                && pathFinder.SearchStatus != SearchStatus.Searching)
            {
                positionMouse = new Vector2(input.CurrentMousestate.X, input.CurrentMousestate.Y);
                Vector2 plus = new Vector2(camera.VisibleArea.Width - 1024, camera.VisibleArea.Height - 768);
                Point mouseTile = mapEngine.Map.PointToTile(positionMouse + plus);

                if (mapEngine.Map.PointToTile(World.Player.Position) == mapEngine.Map.EndTile)
                {
                    mapEngine.Map.StartTile = mapEngine.Map.EndTile;
                    mapEngine.Map.EndTile = mouseTile;
                    pathFinder.Reset();
                    pathFinder.SearchStatus = SearchStatus.Searching;
                    World.Player.Moving = false;
                }
                else
                {
                    mapEngine.Map.StartTile = mapEngine.Map.EndTile;
                    mapEngine.Map.EndTile = mouseTile;
                    pathFinder.Reset();
                    pathFinder.SearchStatus = SearchStatus.Searching;
                    World.Player.Moving = false;
                }

            }
        }

        Vector2 positionMouse = new Vector2();
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
       
           // World.Player.Update(gameTime, camera);
            World.Player.Update(gameTime);
           
            //CheckCollisions();
           
            //CheckCollision();

            pathFinder.Update(gameTime);

           
            if(pathFinder.SearchStatus == SearchStatus.NoPath)
            {
                pathFinder.Reset();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, camera.Matrix);
            mapEngine.DrawLayers(spriteBatch, true, true, false, camera.VisibleArea);
            World.Player.Draw(spriteBatch);
            mapEngine.DrawLayers(spriteBatch, false, false, true, camera.VisibleArea);
            mapEngine.DrawColision(spriteBatch, camera.VisibleArea, ScreenManager.GraphicsDevice);


            pathFinder.Draw(spriteBatch);

            spriteBatch.End();
            spriteBatch.Begin();
            spriteBatch.DrawString(font, "Position: " + mapEngine.Map.PointToTile(World.Player.Position).ToString(), new Vector2(0f, 20f), Color.Yellow);

            spriteBatch.DrawString(font, "Camera: " + camera.VisibleArea.ToString(), new Vector2(0f, 100f), Color.Red);
            Vector2 plus = new Vector2(camera.VisibleArea.Width - 1024, camera.VisibleArea.Height - 768);
            spriteBatch.DrawString(font, "Mouse: " + mapEngine.Map.PointToTile(positionMouse + plus).ToString(),
                                   new Vector2(0f, 130f), Color.Red);
            
            spriteBatch.End();

            //menuPanel.Draw(gameTime);

            miniMap.Draw(gameTime,mapEngine);

            base.Draw(gameTime);
        }


        public void CheckCollision()
        {
            Point point = mapEngine.Map.PointToTile(World.Player.Position);
            foreach (Portal portal in mapEngine.Map.Portals)
            {
                if (point == portal.Position)
                {
                    mapEngine = new MapEngine();
                    mapEngine.Map = new Map();
                    SpriteBatch spriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
                    String strPath = "Content\\Maps\\" + portal.DestinationMap;
                    mapEngine.Map = mapEngine.LoadMap(ScreenManager.Game.Content, strPath);
                    camera = new Camera(new Vector2(mapEngine.Map.MapDimensions.X, mapEngine.Map.MapDimensions.Y),
                        new Vector2(mapEngine.Map.TileSize.X, mapEngine.Map.TileSize.Y),
                        new Vector2(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height));

                    World.Player.Position =
                        new Vector2(portal.DestinationTileLocation.X * mapEngine.Map.TileSize.X,
                                    portal.DestinationTileLocation.Y * mapEngine.Map.TileSize.Y);

                    camera.FocusOn(World.Player);

                    LoadClipMap();
                }
            }
        }
        private void LoadClipMap()
        {
            World.ClipMap = new Dictionary<Vector2, Rectangle>();

            for (int y = 0; y < mapEngine.Map.MapDimensions.Y; y++)
            {
                for (int x = 0; x < mapEngine.Map.MapDimensions.X; x++)
                {
                    Point mapPosition = new Point(x, y);
                    if (mapEngine.Map.IsBlocked(mapPosition))
                    {
                        World.ClipMap.Add(new Vector2(x, y), new Rectangle(x * mapEngine.Map.TileSize.X, y * mapEngine.Map.TileSize.Y, mapEngine.Map.TileSize.X, mapEngine.Map.TileSize.Y));
                    }


                }
            }
        }
        private void CheckCollisions()
        {
            Point pTileLoc = mapEngine.Map.PointToTile(World.Player.Position);
            World.Player.Bounds = new Rectangle((int)(World.Player.Position.X - (mapEngine.Map.TileSize.X / 2)), (int)(World.Player.Position.Y), mapEngine.Map.TileSize.X - (mapEngine.Map.TileSize.X / 16), mapEngine.Map.TileSize.Y - (mapEngine.Map.TileSize.Y / 16)); ;
            Vector2 key = new Vector2(pTileLoc.X, pTileLoc.Y);
            if (World.ClipMap.ContainsKey(key))
            {
                if (World.Player.Bounds.Intersects(World.ClipMap[key]))
                {
                    World.Player.Collision();
                }
            }
        }
       
    }
}
